<template>
  <el-form-item :label="cityName" :required='required'>
    <el-select
      v-if="address.hasOwnProperty('county')"
      v-model="address.county"
      placeholder="请选择"
      class="ne-select-s"
      style="width: 100%"
      @change="getContry"
    >
      <el-option
        v-for="item in countries"
        :key="item.locId"
        :label="item.locName"
        :value="item.locId"
      >
      </el-option>
    </el-select>
    <el-select
      v-if="address.hasOwnProperty('provice')"
      v-model="address.provice"
      placeholder="请选择"
      class="ne-select-s"
      style="width: 100%"
    >
      <el-option
        v-for="item in provices"
        :key="item.locId"
        :label="item.locName"
        :value="item.locId"
      >
      </el-option>
    </el-select>
  </el-form-item>
</template>

<script>
import OrderService from "@/api/order/OrderService.js";
export default {
  props: {
    cityName: {
      type: String,
      default: () => {
        return "";
      },
    },
    address: {
      type: Object,
      default: () => {
        return {};
      },
    },
    required: {
      type: Boolean,
      default:false
    },
  },

  data() {
    return {
      name: "contryOrprovice",
      param: {
        qryType: "", //查询下级的级别  COUNTRY国家 PROVIENCE 省  CITY 市  县
        preLocId: "", //当前选择的 ,上一个组织的locId
        preRegionId: "", //暂时默认不填。当前选择的,preLocId和preRegionId选一个传或都传,上一个组织的RegionId，
      },
      countries: [],
      provices: [],
    };
  },

  created() {
    if(this.address.hasOwnProperty('county')){
        this.getContry();
    }
    if(this.address.hasOwnProperty('provice')){
        this.getProvice();
    }
  },

  methods: {
      getContry(){
        this.param.qryType = "COUNTRY";
        this.param.preLocId = this.address.county;
        this.getCity(this.param).then((result) => {
            if (result.code == 200) {
                this.countries = result.meta.cityList;
                this.countries.forEach((item) => {
                    if (item.locId == this.param.preLocId) {
                        this.address.county = item.locId;
                    }
                });
            }
        });
        this.$emit("func", this.address);
      },
    async getCity(params) {
      //加载城市
      //获取页面下拉框值
      const result = await OrderService.getCityList(params);
      
      return result;
    },
  },
};
</script>

<style scoped>
.ne-select-s {
    margin-right: 0;
}
</style>